package com.welsy.stats.command
{
	import com.welsy.stats.model.StatsModelLocator;
	import com.adobe.cairngorm.commands.ICommand;
	import mx.rpc.IResponder;
	import com.adobe.cairngorm.control.CairngormEvent;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import com.welsy.util.NamespaceLocator;
	import com.welsy.stats.business.GetCourseClassroomsStatsDelegate;
	import com.welsy.stats.event.GetCourseClassroomsStatsEvent;
	import mx.rpc.events.ResultEvent;
	import com.welsy.qnames.AdminResultsDataTypes;
	import com.welsy.stats.vo.CourseClassroomVO;
	
	public class GetCourseClassroomsStatsCommand implements ICommand, IResponder
	{
		[Bindable] private var smodel : StatsModelLocator = StatsModelLocator.getInstance();	
		
		public function execute(event : CairngormEvent):void {
			var delegate : GetCourseClassroomsStatsDelegate = new GetCourseClassroomsStatsDelegate(this);
			var gcc_eve : GetCourseClassroomsStatsEvent = GetCourseClassroomsStatsEvent(event);
			
			delegate.getCourseClassroomsStats(gcc_eve.courseId);
		}
		
		public function result(event : Object) :void {
			var xres : XMLList = new XMLList(ResultEvent(event).result);
			
			smodel.courseClasses = new ArrayCollection;
			var csts_e : XMLList = xres.descendants(AdminResultsDataTypes.TAG_CLASSROOMSTATSCOLLECTION).children();
			for each(var cst_e : XML in csts_e) {
				//ClassroomCollectionType
				var classVO : CourseClassroomVO = new CourseClassroomVO;
				classVO.classId = cst_e.child(AdminResultsDataTypes.TAG_CLASSROOMID);
				classVO.usersCount = cst_e.child(AdminResultsDataTypes.TAG_USERSCOUNT);
				classVO.understandingPerc = cst_e.child(AdminResultsDataTypes.TAG_UNDERSTANDINGPERC);
				classVO.frequencyPerc = cst_e.child(AdminResultsDataTypes.TAG_FREQUNCYPERC);
				classVO.usersCountDo = cst_e.child(AdminResultsDataTypes.TAG_USERSCOUNTDO);
				classVO.title = cst_e.child(AdminResultsDataTypes.TAG_TITLE);
				smodel.courseClasses.addItem(classVO);
			}
		}
		
		public function fault( event : Object ):void {
			Alert.show("GetCourseClassroomsStatsCommand.fault()!");
		}
		
	}
}